﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using AxSHDocVw; // Use `aximp %SystemRoot%\system32\shdocvw.dll`
using IECaptComImports;
using System.IO;

namespace TypedROBIN.Imaging
{
    class IECaptForm : System.Windows.Forms.Form
    {
        internal string mURL;
        internal string mFile;
        internal int mMinWidth;
        internal int mDelay;
        internal AxWebBrowser mWb;
        internal long mStartTicks;

        public IECaptForm(string url, string file, int minWidth, int delay, AxWebBrowser wb)
        {
            mURL = url;
            mFile = file;
            mMinWidth = minWidth;
            mDelay = delay;
            mWb = wb;
            mStartTicks = DateTime.Now.Ticks;
        }

        public void DoCapture()
        {
            IHTMLDocument2 doc2 = (IHTMLDocument2)mWb.Document;
            IHTMLDocument3 doc3 = (IHTMLDocument3)mWb.Document;
            IHTMLElement2 body2 = (IHTMLElement2)doc2.body;
            IHTMLElement2 root2 = (IHTMLElement2)doc3.documentElement;

            // Determine dimensions for the image; we could add minWidth here
            // to ensure that we get closer to the minimal width (the width
            // computed might be a few pixels less than what we want).
            int width = Math.Max(body2.scrollWidth, root2.scrollWidth);
            int height = Math.Max(root2.scrollHeight, body2.scrollHeight);

            // Resize the web browser control
            mWb.SetBounds(0, 0, width, height);

            // Do it a second time; in some cases the initial values are
            // off by quite a lot, for as yet unknown reasons. We could
            // also do this in a loop until the values stop changing with
            // some additional terminating condition like n attempts.
            width = Math.Max(body2.scrollWidth, root2.scrollWidth);
            height = Math.Max(root2.scrollHeight, body2.scrollHeight);
            mWb.SetBounds(0, 0, width, height);

            width = Math.Max(body2.scrollWidth, root2.scrollWidth);
            height = Math.Max(root2.scrollHeight, body2.scrollHeight);
            mWb.SetBounds(0, 0, width, height);
            width = Math.Max(body2.scrollWidth, root2.scrollWidth);
            height = Math.Max(root2.scrollHeight, body2.scrollHeight);
            mWb.SetBounds(0, 0, width, height);

            Bitmap image = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(image);

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

            _RECTL bounds;
            bounds.left = 0;
            bounds.top = 0;
            bounds.right = width;
            bounds.bottom = height;

            IntPtr hdc = g.GetHdc();
            IViewObject iv = doc2 as IViewObject;

            // TODO: Write to Metafile instead if requested.

            iv.Draw(1, -1, (IntPtr)0, (IntPtr)0, (IntPtr)0,
              (IntPtr)hdc, ref bounds, (IntPtr)0, (IntPtr)0, 0);

            g.ReleaseHdc(hdc);
            //image.Save(mFile, CheckOutputFormat());
            ImageCodecInfo ici = new Utils().GetImageCodec(mFile);
            EncoderParameters ep = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);
            image.Save(mFile, ici, ep);
            image.Dispose();
        }

        //private ImageFormat CheckOutputFormat()
        //{
        //    ImageFormat rtn = ImageFormat.Jpeg;
        //    string ext = Path.GetExtension(mFile).ToLowerInvariant();
        //    switch (ext)
        //    {
        //        case ".png":
        //            rtn = ImageFormat.Png;
        //            break;
        //        case ".bmp":
        //        case ".dib":
        //            rtn = ImageFormat.Bmp;
        //            break;
        //        case ".tif":
        //        case ".tiff":
        //            rtn = ImageFormat.Tiff;
        //            break;
        //        case ".gif":
        //            rtn = ImageFormat.Gif;
        //            break;
        //        default:
        //            break;
        //    }
        //    return rtn;
        //}

    }
}

